<template>
  <div class="xtx-goods-page" v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="/">
          {{ goods.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 新增结构 -->

        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :image="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsName></GoodsName>
          <!-- 新增 sku -->
          <GoodsSku :goods="goods" @change="getSku" />
          <XtxNumbox v-model="buyNum" :max="goods.inventory"></XtxNumbox>
          <XtxButton
            type="primary"
            size="large"
            style="margin-top: 20px"
            @click="addCart"
          >
            加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img
              v-imglazy="url"
              v-for="url in goods.details.pictures"
              :key="url"
            />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>

import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/goods.js'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import Message from '@/components/Message/index.js'
import { useStore } from 'vuex'
// 调接口根据id获取商品信息的逻辑函数
const useGetGoods = () => {
  // 购买的数量
  const buyNum = ref(1)
  const route = useRoute()
  const goods = ref({})
  provide('goods', goods)
  async function setgoods () {
    const { result } = await findGoods(route.params.id)
    // console.log('33333333', result)
    goods.value = result
  }
  onMounted(() => {
    setgoods()
  })
  return { buyNum, goods }
}
// 字传父的方法获取到商品的sku信息
const useGetSku = (goods) => {
  // 子传父 获取sku
  const getSku = (sku) => {
    // console.log('sku', sku)
    if (sku.skuId) {
      goods.value.price = sku.price
      goods.value.oldPrice = sku.oldPrice
      goods.value.inventory = sku.inventory
      // 存贮sku信息
      currSel.value = sku
    } else {
      // 如果sku信息没有选全,会返回空对象清除一下当前选中信息
      currSel.value = null
    }
  }
  // 定义当前选中的信息
  const currSel = ref(null)
  return { getSku, currSel }
}
// 添加购物车函数模块
const useAddCart = (currSel, goods, buyNum, store) => {
  // 添加购物车
  const addCart = async () => {
    // 添加选中的到购物车时看有没信息和库存是否充足
    if (!currSel.value) {
      return Message({ text: '请选择相应的商品规格' })
    }
    if (currSel.value.inventory === 0) {
      return Message({ text: '此商品库存不足!' })
    }
    const currGoods = {
      id: goods.value.id,
      name: goods.value.name,
      picture: goods.value.mainPictures[0],
      skuId: currSel.value.skuId,
      price: currSel.value.oldPrice,
      nowPrice: currSel.value.price,
      attrsText: currSel.value.specsText,
      stock: currSel.value.inventory,
      selected: true,
      isEffective: true,
      count: buyNum.value
    }
    console.log('当前商品', currGoods)

    try {
      const data = await store?.dispatch('cart/asyncAddCartList', currGoods)
      // console.log('添加购车车成功===>', data)
      Message({ text: data, type: 'success' })
    } catch (error) {
      console.dir(error)
      Message({ text: '操作失败', type: 'error' })
    }
  }
  return { addCart }
}
export default {
  components: {
    GoodsImage,
    GoodsName,
    GoodsSales
  },
  name: 'XtxGoodsPage',
  setup () {
    const { buyNum, goods } = useGetGoods()
    const { getSku, currSel } = useGetSku(goods)
    const store = useStore() // 只能用在setup第一层函数!!! 不能用在其他函数上 会出现inject()报错
    const { addCart } = useAddCart(currSel, goods, buyNum, store)
    return { goods, getSku, buyNum, addCart, currSel }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
